/* 
 *	Copyright (C) 2009-2010 miaohua
 *	http://code.google.com/p/lrcshower/
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */


#include "stdafx.h"
#include <string>
#include "TypeDef.h"
#include "MusicBox.h"
#include "FileDlg.h"
#include "MFCHelperFuncs.h"
#include "MusicBoxSetting.h"
#include "AssociatedLrcDlg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern MusicBoxSetting Gbl_AppProp;

// CAssociatedLrcDlg dialog

IMPLEMENT_DYNAMIC(CAssociatedLrcDlg, CDialog)

CAssociatedLrcDlg::CAssociatedLrcDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAssociatedLrcDlg::IDD, pParent)
{

}

CAssociatedLrcDlg::~CAssociatedLrcDlg()
{
}

void CAssociatedLrcDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CAssociatedLrcDlg, CDialog)
     ON_BN_CLICKED(IDC_LRCFILE_SEL, &CAssociatedLrcDlg::OnBnClickedLrcFileSel)
     ON_BN_CLICKED(ID_DO_ASSOCIATE, &CAssociatedLrcDlg::OnBnClickedDoAssociate)
     ON_BN_CLICKED(ID_UNDO_ASSOCIATE, &CAssociatedLrcDlg::OnBnClickedUndoAssociate)
     ON_BN_CLICKED(IDCANCEL, &CAssociatedLrcDlg::OnBnClickedCancel)
END_MESSAGE_MAP()


// CAssociatedLrcDlg message handlers
BOOL CAssociatedLrcDlg::OnInitDialog()
{
     CDialog::OnInitDialog();

     tstring LrcFileName = Gbl_AppProp.GetSongLabel() + _T(".lrc");
     GetDlgItem(IDC_SONG_TITLE)->SetWindowText(LrcFileName.c_str());
     GetDlgItem(IDC_SONG_FULLPATH)->SetWindowText(Gbl_AppProp.GetCurSongFullPath().c_str());
     
     return TRUE;
}

void CAssociatedLrcDlg::OnBnClickedLrcFileSel()
{
     // Show lrc file selection dialog
     CString Filters;
     Filters.LoadString(IDS_LRCFILE_FILTER);
	 CFileDlg SelLrcFileDlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST|OFN_OVERWRITEPROMPT, Filters, this);
     // Select lrc file
     if( SelLrcFileDlg.DoModal() != IDOK ) 
         return;
     // Show in GUI
     GetDlgItem(IDC_LRC_FULLPATH)->SetWindowText(SelLrcFileDlg.GetPathName());
}

void CAssociatedLrcDlg::OnBnClickedDoAssociate()
{
     // Get user input: song's full path and lrc file's full path
     tstring SongFullPath, LrcFullPath;
     if( !GetUserInput(SongFullPath, LrcFullPath) )  return;

     // Do association
     SongProp::SetAssignedLrcFile(SongFullPath, LrcFullPath);
}

void CAssociatedLrcDlg::OnBnClickedUndoAssociate()
{
     // Get user input: song's full path and lrc file's full path
     tstring SongFullPath, LrcFullPath;
     if( !GetUserInput(SongFullPath, LrcFullPath) ) return;

     // Undo association
     SongProp::UnSetAssignedLrcFile(SongFullPath, LrcFullPath);
}

void CAssociatedLrcDlg::OnBnClickedCancel()
{
     OnCancel();
}

bool CAssociatedLrcDlg::GetUserInput(tstring & SongFullPath, tstring & LrcFullPath)
{
     TCHAR FullPath[MAX_PATH];
     GetDlgItem(IDC_SONG_FULLPATH)->GetWindowText(FullPath, MAX_PATH);
     SongFullPath = FullPath;
     GetDlgItem(IDC_LRC_FULLPATH)->GetWindowText(FullPath, MAX_PATH);
     LrcFullPath = FullPath;

     if( SongFullPath.empty() || LrcFullPath.empty() )
     {
         Msg_Prompt(IDS_ASSOCIATION_ERR_TIP, MB_OK|MB_ICONERROR|MB_APPLMODAL);
         return false;
     }
       
     return true;
}